home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / DRIVES.SWG / 0061_Media ID.pas < prev    next >
Pascal/Delphi Source File  |  1994-01-27  |  4KB  |  111 lines

  1. {
  2. > Can someone please post some code on how to read a disk label/serial
  3. > number from a disk. I plan to use it as a copy protection method (read
  4. > the label/serial number on installation and only the program to install
  5. > on a drive the same label/serial number) Thanks!
  6.  
  7. Do you realise that the serial number on a disk is changed when the disk is
  8. formatted?  Therefore if someone crashes their system and has to format their
  9. hard disk and restore your software from their backups your protection method
  10. would be triggered!  Not a very good method to use for copy protection.
  11. }
  12. Program     MediaID;
  13.  
  14. Uses
  15.   Dos;
  16.  
  17. Type
  18.   Tmid = record
  19.     midInfoLevel   : Word;                        { information level ? }
  20.     midSerialNum   : LongInt;                           { serial number }
  21.     midVolLabel    : packed array [1..11] of Char; { ASCII volume label }
  22.     midFileSysType : packed array [1..8] of Char;   { ASCII file system }
  23.   end;   { of Tmid }
  24.  
  25. Var
  26.   MID : Tmid;
  27.   DriveChar : Char;
  28.   DriveNum : Word;
  29.   DirInfo : SearchRec;
  30.   Volume : String;
  31.  
  32.   Function    Hex4(w : Word) : String;
  33.   const
  34.     HexStr : packed array [$00..$0F] of Char = '0123456789ABCDEF';
  35.   var
  36.     s : String;
  37.     ndx : Integer;
  38.   begin  { of Hex4 }
  39.     s := '';
  40.     for ndx := 3 downto 0 do
  41.       begin
  42.         s := s + HexStr[(W shr (ndx*4)) and $0F];
  43.       end;
  44.     Hex4 := s;
  45.   end;   { of Hex4 }
  46.  
  47.   Function    GetMediaID(Drive : Word) : Word;
  48.   {---------------------------------------------------------------------}
  49.   {    This routine reads the VolumeLabel, SerialNumber from the boot   }
  50.   {  sector of the specified drive.  Requires MSDOS5 or above.          }
  51.   {---------------------------------------------------------------------}
  52.   begin  { of GetMediaID }
  53.     asm
  54.       mov   bx, Drive                       { 0=default, 1=A:, 2=B: etc }
  55.       mov   ch, 08h                     { device category (must be 08h) }
  56.       mov   cl, 66h                                      { Get Media ID }
  57.       mov   dx, seg MID                { ds:dx pointer to MID structure }
  58.       mov   ds, dx
  59.       mov   dx, offset MID
  60.       mov   ax, 440Dh                          { IOCTL for block device }
  61.       int   21h
  62.       jc    @1                      { carry is set if there is an error }
  63.       mov   ax, 0000h                             { no error - clear ax }
  64.     @1:
  65.       mov   @result, ax                             { return error code }
  66.     end;
  67.   end;   { of GetMediaID }
  68.  
  69.   Function    VolumeLabel(Drive : Char; var VolLabel : String) : Word;
  70.   {---------------------------------------------------------------------}
  71.   {    This routine reads the VolumeLabel from the root directory of    }
  72.   {  the specified drive.                                               }
  73.   {---------------------------------------------------------------------}
  74.   begin  { of VolLabel }
  75.     FindFirst(Drive+':\*.*', VolumeID, DirInfo);
  76.     VolumeLabel := DosError;
  77.     VolLabel := DirInfo.Name;
  78.     { delete a "." which would be the 9th character }
  79.     if (Length(VolLabel) > 8) then
  80.       Delete(VolLabel, 8, 1);
  81.   end;   { of VolLabel }
  82.  
  83. begin  { of MediaID }
  84.  
  85.   DriveChar := 'C';
  86.   DriveNum := ord(DriveChar) - 64;
  87.  
  88.   if (GetMediaID(DriveNum) = 0) then
  89.     begin
  90.       Writeln(output, 'InfoLevel = ', MID.midInfoLevel);
  91.       Writeln(output, 'SerialNum = ',
  92.         Hex4((MID.midSerialNum shr $10) and $FFFF), '-',
  93.         Hex4(MID.midSerialNum and $FFFF));
  94.       Writeln(output, 'VolLabel    = "', MID.midVolLabel, '"');
  95.       Writeln(output, 'FileSysType = "', MID.midFileSysType, '"');
  96.     end
  97.   else
  98.     begin
  99.       { function not supported or error }
  100.     end;
  101.  
  102.   Writeln(output);
  103.  
  104.   if (VolumeLabel(DriveChar, Volume) = 0) then
  105.     Writeln(output, 'VolLabel    = "', Volume, '"')
  106.   else
  107.     begin
  108.       { error }
  109.     end;
  110. end.   { of MediaID }
  111.